﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OA.Mvc3.Controllers
{
    [ValidateInput(false)]
    public class DialogController :UI.BaseController
    {
        public const string ProgrameCode = "Dialog";
        public static readonly string currentPath = UI.Utils.GetCurrentPath(ProgrameCode);

        public DialogController()
        {
            ViewBag.CurrentPath = currentPath;
        }

        [UI.ValidateAuthority(ProgrameCode)]
        public ActionResult Index()
        {
            return View();
        }

        [UI.ValidateAuthority(ProgrameCode)]
        public string List(int page, int rows, string sort, string order)
        {
            string where = Common.HqlQeuryHelp.GetWhere(typeof(Model.Dialog), "DialogCode,Title");
            string orderby = null;
            if (!string.IsNullOrEmpty(sort)) orderby = sort + " " + order;

            var bll = new BLL.Dialog();
            var list = bll.Get(where, orderby, (page - 1) * rows, rows);
            List<object> listView = new List<object>();
            foreach (var i in list)
            {
                listView.Add(new
                {
                    DialogCode = i.DialogCode,
                    Title = i.Title,
                    Database=i.Database,
                    Width=i.Width,
                    Height=i.Height,
                    ResultFieldKey=i.ResultFieldKey,
                    FieldCount = i.Fields.Count
                });
            }
            var ret = new
            {
                total=bll.GetCount(where),
                rows = listView
            };
            return UI.Utils.ToJsonString(ret);
        }

        [UI.ValidateAuthority(ProgrameCode , "delete")]
        public string Delete(string id)
        {
            var ret = new BLL.Dialog().Delete(id);
            return UI.Utils.ToJsonString(ret);
        }

        [UI.ValidateAuthority(ProgrameCode , "add")]
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        [UI.ValidateAuthority(ProgrameCode , "add")]
        public string Add(Model.Dialog model)
        {
            var ret=new BLL.Dialog().Add(model);
            return UI.Utils.ToJsonString(ret);
            
        }

        [UI.ValidateAuthority(ProgrameCode , "edit")]
        public ActionResult Edit(string  DialogCode)
        {
            var model = new BLL.Dialog().Get(DialogCode);
            if (model == null) return UI.Utils.ErrorDateNoExist();
            return View(model);
        }

        [HttpPost]
        [UI.ValidateAuthority(ProgrameCode , "edit")]
        public string Edit(Model.Dialog model)
        {
            var ret = new BLL.Dialog().Update(model);
            return UI.Utils.ToJsonString(ret);
        }


        public string GetDialog(string dialogCode)
        {
            var model = new BLL.Dialog().Get(dialogCode);
            if (model == null) return null;

            var ret = new
            {
                DialogCode = model.DialogCode,
                Title = model.Title,
                Width = model.Width,
                Height = model.Height,
                ResultFieldKey=model.ResultFieldKey
            };
            return UI.Utils.ToJsonString(ret);
        }
    }
}
